//
//  TrainingViewController.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/19.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "TrainingViewController.h"
#import "APIService.h"
#import "APIService+Login.h"
#import "AppDelegate.h"
#import "MyPlanViewController.h"
#import "TrainingProjectViewController.h"
#import "SpineTestViewController.h"

@interface TrainingViewController ()

@property (nonatomic, strong) UIButton * myPlanButton;
@property (nonatomic, strong) UIButton * trainingProjectButton;
@property (nonatomic, strong) UIButton * spineTestButton;

@property (nonatomic, strong) RACCommand * myPlanCommand;
@property (nonatomic, strong) RACCommand * trainingProjectCommand;
@property (nonatomic, strong) RACCommand * spineTestCommand;

@property (nonatomic, assign) BOOL userHadTested;

@end

@implementation TrainingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewSetting
{
    [super viewSetting];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_quit"] style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
    [self.view addSubview:self.myPlanButton];
    [self.view addSubview:self.trainingProjectButton];
    [self.view addSubview:self.spineTestButton];
    
    self.myPlanButton.rac_command = self.myPlanCommand;
    self.trainingProjectButton.rac_command = self.trainingProjectCommand;
    self.spineTestButton.rac_command = self.spineTestCommand;
}

- (void)bindViewModel
{
    [super bindViewModel];
}

- (void)makeConstaints
{
    [super makeConstaints];
    WS(weakSelf);
    [self.myPlanButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((screenHeight - TabBarHeight)/3));
        make.width.equalTo(@(screenWidth));
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];
    [self.trainingProjectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((screenHeight - TabBarHeight)/3));
        make.width.equalTo(@(screenWidth));
        make.top.equalTo(weakSelf.myPlanButton.mas_bottom);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];
    [self.spineTestButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((screenHeight - TabBarHeight)/3));
        make.width.equalTo(@(screenWidth));
        make.top.equalTo(weakSelf.trainingProjectButton.mas_bottom);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];
}

- (void)logout
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"退出登录" message:@"是否确定退出登录" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert.rac_buttonClickedSignal subscribeNext:^(NSNumber * x) {
        if ([x integerValue] == 1) {
            [[[APIService shareService] logout] subscribeNext:^(id x) {
                [(AppDelegate *)[UIApplication sharedApplication].delegate showLoginView];
            }];
        }
    }];
    [alert show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Getter
- (UIButton *)myPlanButton
{
    if (!_myPlanButton) {
        _myPlanButton = [[UIButton alloc] init];
        [_myPlanButton setTitle:@"我的计划" forState:UIControlStateNormal];
        [_myPlanButton setTitleColor:[UIColor colorWithRGBHex:0x24B999] forState:UIControlStateNormal];
        [_myPlanButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    return _myPlanButton;
}

- (UIButton *)trainingProjectButton
{
    if (!_trainingProjectButton) {
        _trainingProjectButton = [[UIButton alloc] init];
        [_trainingProjectButton setTitle:@"锻炼项目" forState:UIControlStateNormal];
        [_trainingProjectButton setTitleColor:[UIColor colorWithRGBHex:0x24B999] forState:UIControlStateNormal];
        [_trainingProjectButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    return _trainingProjectButton;
}

- (UIButton *)spineTestButton
{
    if (!_spineTestButton) {
        _spineTestButton = [[UIButton alloc] init];
        [_spineTestButton setTitle:@"脊柱评估测试" forState:UIControlStateNormal];
        [_spineTestButton setTitleColor:[UIColor colorWithRGBHex:0x24B999] forState:UIControlStateNormal];
        [_spineTestButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    return _spineTestButton;
}

- (RACCommand *)myPlanCommand
{
    if (!_myPlanCommand) {
        @weakify(self); 
        _myPlanCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            if ([APIService shareService].user.hadSpineTested && [APIService shareService].spineTestData.neckPoint < 100 && [APIService shareService].spineTestData.waistPoint < 100) {
                MyPlanViewModel * viewModel = [[MyPlanViewModel alloc] init];
                MyPlanViewController * vc = [[MyPlanViewController alloc] initWithViewModel:viewModel];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else {
                UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"请先完成脊柱评估测试" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"颈椎",@"腰椎", nil];
                [actionsheet showInView:self.view];
            }
            return [RACSignal empty];
        }];
    }
    return _myPlanCommand;
}

- (RACCommand *)trainingProjectCommand
{
    if (!_trainingProjectCommand) {
        @weakify(self);
        _trainingProjectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            /*
            if ([APIService shareService].user.hadSpineTested && [APIService shareService].spineTestData.neckPoint < 100 && [APIService shareService].spineTestData.waistPoint < 100) {
                TrainingProjectViewModel * viewModel = [[TrainingProjectViewModel alloc] init];
                TrainingProjectViewController * vc = [[TrainingProjectViewController alloc] initWithViewModel:viewModel];
                [self.navigationController pushViewController:vc animated:YES];
            }
            else {
                UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"请先完成脊柱评估测试" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"颈椎",@"腰椎", nil];
                [actionsheet showInView:self.view];
            }
             */
            TrainingProjectViewModel * viewModel = [[TrainingProjectViewModel alloc] init];
            TrainingProjectViewController * vc = [[TrainingProjectViewController alloc] initWithViewModel:viewModel];
            [self.navigationController pushViewController:vc animated:YES];
            return [RACSignal empty];
        }];
    }
    return _trainingProjectCommand;
}

- (RACCommand *)spineTestCommand
{
    if (!_spineTestCommand) {
        @weakify(self);
        _spineTestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            
            UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"选择测试部位" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"颈椎",@"腰椎", nil];
            [actionsheet showInView:self.view];
            return [RACSignal empty];
        }];
    }
    return _spineTestCommand;
}

#pragma mark - UIActionSheet Delegate - 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        SpineTestViewModel * viewModel = [[SpineTestViewModel alloc] init];
        viewModel.testType = 0;
        SpineTestViewController * vc = [[SpineTestViewController alloc] initWithViewModel:viewModel];
        [vc createCervicalVertebraQuestionaire];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if(buttonIndex == 1)
    {
        SpineTestViewModel * viewModel = [[SpineTestViewModel alloc] init];
        viewModel.testType = 1;
        SpineTestViewController * vc = [[SpineTestViewController alloc] initWithViewModel:viewModel];
        [vc createLumbarVertebraQuestionaire];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

@end
